package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.DormitoryStudentBean;
import com.mentaleye.bean.DormitorysBean;
import com.mentaleye.biz.DormitoryBiz;
import com.mentaleye.service.DormitoryService;
import com.mentaleye.service.DormitoryStudentService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
@Service
public class DormitoryBizImpl implements DormitoryBiz {

	@Autowired private DormitoryService dormitoryService;
	@Autowired private DormitoryStudentService dormitoryStudentService;
	private static Logger logger = Logger.getLogger(DormitoryBizImpl.class);
	@Override
	public CommonResult editDormitory(DormitorysBean dormitoryBean) {
		CommonResult result = new CommonResult();
		boolean success=false;
		try {
			if(dormitoryBean.getDorId()==null){
				success=dormitoryService.addDormitory(dormitoryBean);
			}else{
				success=dormitoryService.editDormitory(dormitoryBean);
			}
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("编辑宿舍出现异常", e);
		}
		return result;
	}

	@Override
	public CommonResult removeDormitory(List<Integer> dorId) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			success = dormitoryService.removeDormitory(dorId);
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("删除宿舍出现异常", e);
		}
		return result;
	}

	@Override
	public List<DormitorysBean> getAllDirmitory() {
		List<DormitorysBean> list = new ArrayList<DormitorysBean>();
		try {
			list = dormitoryService.getAllDirmitory();
		} catch (Exception e) {
			logger.error("查询全部宿舍出现异常", e);
		}
		return list;
	}

	@Override
	public void queryDormitoryByPage(CommonPage commonPage) {
		try {
			dormitoryService.getDormitoryByPage(commonPage);
		} catch (Exception e) {
			logger.error("分页查询全部宿舍出现异常", e);
		}
		
		
	}

	@Override
	public DormitorysBean queryDormitoryById(Integer dorId) {
		try {
			return dormitoryService.getDormitoryById(dorId);
		} catch (Exception e) {
			logger.error("通过编号查询宿舍异常", e);
			return null;
		}
	}

	@Override
	public CommonResult queryStudentByDormitory(List<Integer> dorId) {
		CommonResult result = new CommonResult();
		boolean success = true;
		List<DormitoryStudentBean> list = new ArrayList<DormitoryStudentBean>();
		for (Integer id : dorId) {
			 list = dormitoryStudentService.getDormitoryRelateStudentById(id);
			 if(list!=null && list.size()>0){
				 success = false;
				 break;
			 }else{
				 success = true;
			 }
		}
		result.setIsSuccess(success);
		return result;
	}

	@Override
	public List<DormitorysBean> queryDormitoryWithRest() {
		try {
			return dormitoryService.getDormitoryWithRest();
		} catch (Exception e) {
			logger.error("查询可用宿舍的异常", e);
			return null;
		}
	}

}
